package com.netty.source_code.memory_pool;

import io.netty.util.internal.ObjectPool;

/**
 * @author xl-9527
 * @since 2025/7/19
 **/
public class ObjectPoolDemo {

    public static void main(String[] args) {
        final ObjectPool<User> objectPool = ObjectPool.newPool(User::new);

        // 获取对象
        final User user = objectPool.get();
        user.name = "xl-9527";
        user.release();
        System.out.println("使用 Object pool 来完成对象的池化" + objectPool.get().name);
    }

    private static class User {
        private String name;

        private final ObjectPool.Handle<User> handle;

        private User(final ObjectPool.Handle<User> handle) {
            this.handle = handle;
        }

        public void release() {
            handle.recycle(this);
        }
    }
}
